//
//  SDHSportsPlayerInfoVC.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/11.
//

#import "SDHSportsPlayerInfoVC.h"
#import "SDHSportsPlayerInfoCell.h"
#import "SDHSportsPlayInfoDetailView.h"
#import "SDHSportsNetRequest.h"
#import "SDHSportsRequest.h"
#import "SDHSportsPlayerModel.h"
#import "SDHSportsPlayerInfoModel.h"
#import "MJExtension.h"
#import "SDHSportsPlayerInfoHeader.h"

@interface SDHSportsPlayerInfoVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *dataTable;
@property (nonatomic, strong) SDHSportsPlayerInfoCell *characteristicCell;
@property (nonatomic, strong) SDHSportsPlayerInfoCell *currentTeamCell;
@property (nonatomic, strong) SDHSportsPlayerInfoCell *transferRecordCell;

@property (nonatomic, strong) SDHSportsPlayerInfoHeader *headerView;
@property (nonatomic, strong) SDHSportsPlayerInfoModel *playerModel;
@property (nonatomic, strong) NSMutableArray *transferRecordArr;

@end

@implementation SDHSportsPlayerInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.title = @"Player Information";
    _transferRecordArr = [NSMutableArray array];
    [self initUI];
    [self initCell];
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewWillLayoutSubviews {
    self.dataTable.tableHeaderView.frame = CGRectMake(0, 0, kScreenWidth, kPlayInfoTableHeaderHeight);
}

- (void)initUI {
    UITableView *table = [UITableView new];
    table.rowHeight = UITableViewAutomaticDimension;
    table.estimatedRowHeight = 150;
    table.delegate = self;
    table.dataSource = self;
    table.backgroundColor = [UIColor blackColor];
    [self.view addSubview:table];
    self.dataTable = table;
    
    [table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsZero);
    }];

    _headerView = [SDHSportsPlayerInfoHeader new];
    table.tableHeaderView = _headerView;
    UIView *footerview = [UIView new];
    table.tableFooterView = footerview;
}

- (void)initCell {
    SDHSportsPlayerInfoCell *cell = [[SDHSportsPlayerInfoCell alloc] initWithStyle:UIAlertActionStyleDefault reuseIdentifier:nil];
    cell.titleLb.text = @"Player characteristics";
    cell.infoView1.title = @"Style";
    cell.infoView1.detail = @"";
    cell.infoView2.title = @"Advantage";
    cell.infoView2.detail = @"Penalty kick Unload the ball";
    cell.infoView3.title = @"Disadvantage";
    cell.infoView3.detail = @"";
    self.characteristicCell = cell;
    
    cell = [[SDHSportsPlayerInfoCell alloc] initWithStyle:UIAlertActionStyleDefault reuseIdentifier:nil];
    cell.titleLb.text = @"Current team";
    cell.infoView1.title = nil;
    cell.infoView1.detail = @"Manchester United";
    cell.infoView2.title = nil;
    cell.infoView2.detail = @"10";
    cell.infoView3.title = nil;
    cell.infoView3.detail = @"Forward";
    self.currentTeamCell = cell;
    
    cell = [[SDHSportsPlayerInfoCell alloc] initWithStyle:UIAlertActionStyleDefault reuseIdentifier:nil];
    cell.titleLb.text = @"Transfer record";
    cell.transferLb.hidden = NO;
    cell.infoView1.hidden = YES;
    cell.infoView2.hidden = YES;
    cell.infoView3.hidden = YES;
    self.transferRecordCell = cell;
}

- (void)loadData {
    SDHSportsRequest *request = [[SDHSportsRequest alloc] init];
    request.requestUrl = @"http://jsuywu8.com/guojitiyu/Tiyusaishi/playerdata";
    NSString *player_id = _model.player_id;
    NSString *team_id = _model.team_id;
    if (player_id.length == 0 || team_id.length == 0) {
        player_id = @"73090";
        team_id = @"99686";
    }
    request.requestPara = @{@"team_id":team_id, @"player_id":player_id};
    
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD showLoading];
    [[SDHSportsNetRequest sharedRequest] sendRequest:request returnValueBlock:^(id  _Nonnull obj) {
        [weakSelf parseData:obj];
        
        [weakSelf endLoading];
    } errorBlock:^(id  _Nonnull obj) {
        [weakSelf endLoading];
    } failureBlock:^{
        [weakSelf endLoading];
    }];
    
    
    SDHSportsRequest *req = [[SDHSportsRequest alloc] init];
    req.requestUrl = @"http://jsuywu8.com/guojitiyu/Tiyusaishi/getPlayerTransfer";
    req.requestPara = @{@"player_id":player_id};
    [[SDHSportsNetRequest sharedRequest] sendRequest:req returnValueBlock:^(id  _Nonnull obj) {
        [weakSelf parseTransferRecord:obj];
    } errorBlock:^(id  _Nonnull obj) {
        
    } failureBlock:^{
        
    }];
}

- (void)parseData:(NSDictionary *)dic {
    _playerModel = [SDHSportsPlayerInfoModel mj_objectWithKeyValues:dic[@"data"]];
}

- (void)parseTransferRecord:(NSDictionary *)dic {
    [_transferRecordArr removeAllObjects];
    [_transferRecordArr addObjectsFromArray:dic[@"data"]];
    
    [self.dataTable reloadData];
}

- (NSString *)getTransferRecord {
    NSMutableString *transferRecordStr = [NSMutableString string];
    for (NSDictionary *tmpDic in _transferRecordArr) {
        NSArray *allkeys = [tmpDic allKeys];
        NSString *tmpStr = @"";
        for (NSString *key in allkeys) {
            tmpStr = [NSString stringWithFormat:@"%@\n%@: %@", tmpStr, key, tmpDic[key]];
        }
        
        [transferRecordStr appendFormat:@"%@\n\n", tmpStr];
    }
    
    return transferRecordStr;
}

- (void)endLoading {
    [MBProgressHUD hideHUD];
    [self updateHeader];
    [self.dataTable reloadData];
}

- (void)updateHeader {
    [_headerView updateContent:_playerModel];
}

#pragma mark -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (2 == indexPath.row) {
        return UITableViewAutomaticDimension;
    }
    
    return 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SDHSportsPlayerInfoCell *cell = nil;
    
    if (0 == indexPath.row) {
        cell = self.characteristicCell;
        NSString *style = [_playerModel.style componentsJoinedByString:@"\n"];
        NSString *advantage = [_playerModel.advantage componentsJoinedByString:@"\n"];
        NSString *disadvantage = [_playerModel.weakness componentsJoinedByString:@"\n"];
        self.characteristicCell.infoView1.detail = style;
        self.characteristicCell.infoView2.detail = advantage;
        self.characteristicCell.infoView3.detail = disadvantage;
    } else if (1 == indexPath.row) {
        cell = self.currentTeamCell;
        self.currentTeamCell.infoView1.detail = _playerModel.team_en_name;
        self.currentTeamCell.infoView2.detail = _playerModel.number;
        self.currentTeamCell.infoView3.detail = _playerModel.position;
    } else {
        cell = self.transferRecordCell;
        
        self.transferRecordCell.transferLb.text = [self getTransferRecord];
    }
    
    return cell;
}

@end
